String.format = function() {
    if (arguments.length == 0)
        return null;
    var str = arguments[0];
    for ( var i = 1; i < arguments.length; i++) {
        var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
    /*
    调用方式：
        var info = "我喜欢吃{0}，也喜欢吃{1}，但是最喜欢的还是{0},偶尔再买点{2}。";
        var msg=String.format(info , "苹果","香蕉","香梨")
        alert(msg);
        输出:我喜欢吃苹果，也喜欢吃香蕉，但是最喜欢的还是苹果,偶尔再买点香梨。
    */
};


function loadingGif(loadingGif) {
    //加载请求时旋转动态图片
    var imgId = document.getElementById(loadingGif);
    if (imgId.style.display == "block") {
        imgId.style.display = "none";
    }
    else {
        imgId.style.display = "block";
    }
}


function resize_im(w, h, scale, max_scale) {
    f = parseFloat(scale) / Math.min(h, w);
    if (f * Math.max(h, w) > max_scale) {
        f = parseFloat(max_scale) / Math.max(h, w);
    }
    newW = parseInt(w * f);
    newH = parseInt(h * f);

    return [newW, newH, f]
}

function getChildDetail() {
    jQuery("#billmodeltable").remove();
    childResult = imgJson["result"];
    createTable(childResult, imgJson['timeTake']);//新建table
}


 //根据获取的数据，创建table
    //创建table
function createTable(result, timeTake) {
    //根据获取的数据，创建table
    jQuery("#tbody").empty();
    var jsObject = result;
    imgBoxes = [];
    $("#taketime").empty();
    $("#taketime").append("<p>耗时:" + timeTake + "秒 </p>");
    var tablestr = "";
    for (var i = 0; i < jsObject.length; i++) {
        tablestr += String.format("<tr><td>{0}</td><td>{1}</td></tr>", jsObject[i]["name"], jsObject[i]["text"]);
        imgBoxes.push(jsObject[i]["box"]);
    }
    $("#tbody").append(tablestr);
    drawRbox(imgBoxes, 'myCanvas');
}

function drawRbox(boxes, canvasId) {
    /*canvas上绘制倾斜矩形
     */
    var canvas = document.getElementById(canvasId);
    if (canvas.getContext) {
        var ctx = canvas.getContext("2d");
        ctx.strokeStyle = 'rgba(255,0,0,0.5)';
        ctx.lineWidth = 2;
        // ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.beginPath();
        for (var i = 0; i < imgBoxes.length; i++) {
            box = imgBoxes[i];//x1,y1,x2,y2,x3,y3,x4,y4
            x1 = box[0] * imgRate;
            y1 = box[1] * imgRate;
            x2 = box[2] * imgRate;
            y2 = box[3] * imgRate;
            x3 = box[4] * imgRate;
            y3 = box[5] * imgRate;
            x4 = box[6] * imgRate;
            y4 = box[7] * imgRate;

            ctx.moveTo(x1, y1);
            ctx.lineTo(x2, y2);

            ctx.moveTo(x2, y2);
            ctx.lineTo(x3, y3);

            ctx.moveTo(x3, y3);
            ctx.lineTo(x4, y4);

            ctx.moveTo(x4, y4);
            ctx.lineTo(x1, y1);
            ctx.stroke();
        }

        ctx.closePath();
    }
}

